<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改公司基本信息')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-company-edit" th:object="${company}">
            <input name="cpId" th:field="*{cpId}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">公司名称：</label>
                    <div class="col-sm-8">
                        <input name="cpName" th:field="*{cpName}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">状态：</label>
                    <div class="col-sm-8">
                        <input name="cpStatus" th:field="*{cpStatus}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">插入时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="cpInsdate" th:value="${#dates.format(company.cpInsdate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">联系电话：</label>
                    <div class="col-sm-8">
                        <input name="cpPhone" th:field="*{cpPhone}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">公司简介：</label>
                    <div class="col-sm-8">
                        <textarea name="cpInfo" class="form-control">[[*{cpInfo}]]</textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">轮播图：</label>
                    <div class="col-sm-8">
                        <input type="hidden" name="cpTitelImg" th:field="*{cpTitelImg}">
                       <div class="file-loading">
                            <input class="form-control file-upload" id="cpTitelImg" name="file" type="file" multiple>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">公司地址：</label>
                    <div class="col-sm-8">
                        <input name="cpAddress" th:field="*{cpAddress}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">公司编码：</label>
                    <div class="col-sm-8">
                        <input name="cpCode" th:field="*{cpCode}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">宣传视频：</label>
                    <div class="col-sm-8">
                        <input type="hidden" name="cpVideo" th:field="*{cpVideo}">
                       <div class="file-loading">
                            <input class="form-control file-upload" id="cpVideo" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">文化宣传：</label>
                    <div class="col-sm-8">
                        <textarea name="cpCulture" class="form-control">[[*{cpCulture}]]</textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">公司类型：</label>
                    <div class="col-sm-8">
                        <input name="cpType" th:field="*{cpType}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">实力信息：</label>
                    <div class="col-sm-8">
                        <textarea name="cpStrength" class="form-control">[[*{cpStrength}]]</textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">发展领域信息：</label>
                    <div class="col-sm-8">
                        <textarea name="cpLingyu" class="form-control">[[*{cpLingyu}]]</textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">产品制造：</label>
                    <div class="col-sm-8">
                        <textarea name="cpManufacture" class="form-control">[[*{cpManufacture}]]</textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">公司程序码：</label>
                    <div class="col-sm-8">
                        <input type="hidden" name="cpXcxm" th:field="*{cpXcxm}">
                        <div class="file-loading">
                            <input class="form-control file-upload" id="cpXcxm" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/company";
        $("#form-company-edit").validate({
            focusCleanup: true
        });
        function isValidJson(jsonString) {
            try {
                JSON.parse(jsonString);
            } catch (e) {
                return false;
            }
            return true;
        }
        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-company-edit').serialize());
            }
        }

        $("input[name='cpInsdate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            if(isValidJson(val)){
                var previewUrls = JSON.parse(val);
            }else{
                previewUrls = [val];
            }
            // var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: previewUrls,
                maxFileCount: 10,
                autoReplace: true,
                multiple:true
            }).on('fileuploaded', function (event, data, previewId, index) {
                // $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
                var $inputField = $("input[name='" + event.currentTarget.id + "']");
                // 获取当前输入框的值，如果不存在则初始化为空数组
                var currentUrls =isValidJson($inputField.val()) && $inputField.val() ? JSON.parse($inputField.val()) : [];

                // 将新的图片URL追加到现有URL数组中
                console.log(data.response.url,"--------------------")

                currentUrls.push(data.response.url);

                // 将更新后的URL数组转换为JSON字符串并存回输入框
                $inputField.val(JSON.stringify(currentUrls));
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });
    </script>
</body>
</html>